package sundun.sharding.interceptor;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import sundun.sharding.execution.Executions;

import java.sql.Statement;
import java.util.*;

/**
 * 拦截器
 * @author: zxl
 * @date: 2024/8/11 21:22
 */
//@Intercepts(
//        {
//                @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
//                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
//                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
//        }
//)
@Intercepts({
        @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
        @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
        @Signature(type = StatementHandler.class, method = "batch", args = { Statement.class })})
public class ShardingAdapterInterceptor implements Interceptor {
    //执行器 spring方式注入
    private Executions executions;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        if (Objects.isNull(executions)) return invocation.proceed();
        return executions.proxyExecute(invocation);
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        //do nothing
    }

    public void setExecutions(Executions executions) {
        this.executions = executions;
    }



}
